//+------------------------------------------------------------------+
//|                                                TimeWindowTest.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd |
//|                                        https://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd"
#property link      "https://www.metaquotes.net"
#property version   "1.00"
#property strict

//--- 输入参数
input int NewDayOpenTimeMinutes = 5;   // 日K线开盘后允许开仓的分钟数（00:00-00:04，共5分钟）

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Print("=======================================");
   Print("时间窗口逻辑测试开始");
   Print("开仓时间窗口设置: 00:00-00:", (NewDayOpenTimeMinutes-1), " (共", NewDayOpenTimeMinutes, "分钟)");
   
   // 测试各种时间点
   TestTimeWindow(0, 0);   // 00:00 - 应该在窗口内
   TestTimeWindow(0, 1);   // 00:01 - 应该在窗口内
   TestTimeWindow(0, 4);   // 00:04 - 应该在窗口内
   TestTimeWindow(0, 5);   // 00:05 - 应该不在窗口内
   TestTimeWindow(1, 0);   // 01:00 - 应该不在窗口内
   TestTimeWindow(1, 5);   // 01:05 - 应该不在窗口内 (用户发现的问题)
   TestTimeWindow(23, 59); // 23:59 - 应该不在窗口内
   
   Print("时间窗口逻辑测试完成");
   Print("=======================================");
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 测试指定时间是否在开仓窗口内                                        |
//+------------------------------------------------------------------+
void TestTimeWindow(int hour, int minute)
{
   // 模拟时间窗口判断逻辑（与SimpleMartingale.mq4中的逻辑一致）
   bool isWithinWindow = (hour == 0 && minute < NewDayOpenTimeMinutes);
   
   // 格式化时间显示
   string timeStr = (hour < 10 ? "0" : "") + IntegerToString(hour) + ":" + 
                    (minute < 10 ? "0" : "") + IntegerToString(minute);
   
   // 输出测试结果
   string result = isWithinWindow ? "在窗口内 ✓" : "不在窗口内 ✗";
   string expected = "";
   
   // 判断预期结果
   if(hour == 0 && minute < NewDayOpenTimeMinutes)
   {
      expected = "预期: 在窗口内";
   }
   else
   {
      expected = "预期: 不在窗口内";
   }
   
   // 检查结果是否符合预期
   bool isCorrect = ((hour == 0 && minute < NewDayOpenTimeMinutes) == isWithinWindow);
   string status = isCorrect ? "[正确]" : "[错误]";
   
   Print("测试时间: ", timeStr, " -> ", result, " (", expected, ") ", status);
   
   // 特别标注01:05的测试结果
   if(hour == 1 && minute == 5)
   {
      if(!isWithinWindow)
      {
         Print("✓ 重要: 01:05正确地被判断为不在开仓窗口内，问题已修复");
      }
      else
      {
         Print("✗ 错误: 01:05仍然被错误判断为在开仓窗口内！");
      }
   }
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // 获取当前时间并测试窗口逻辑（仅在整分钟时输出一次）
   datetime currentTime = TimeCurrent();
   int currentHour = TimeHour(currentTime);
   int currentMinute = TimeMinute(currentTime);
   int currentSecond = TimeSeconds(currentTime);
   
   // 每分钟的第一个tick输出当前时间窗口状态
   if(currentSecond == 0)
   {
      bool isWithinWindow = (currentHour == 0 && currentMinute < NewDayOpenTimeMinutes);
      string timeStr = (currentHour < 10 ? "0" : "") + IntegerToString(currentHour) + ":" + 
                       (currentMinute < 10 ? "0" : "") + IntegerToString(currentMinute);
      
      Print("实时检查 ", timeStr, ": ", (isWithinWindow ? "在开仓窗口内" : "不在开仓窗口内"));
      
      // 如果是关键时间点，提供额外信息
      if((currentHour == 0 && currentMinute == NewDayOpenTimeMinutes) || 
         (currentHour == 1 && currentMinute == 5))
      {
         Print("关键时间点检查: ", timeStr, " -> ", (isWithinWindow ? "在窗口内" : "不在窗口内"));
      }
   }
}

//+------------------------------------------------------------------+